#-------------------------------------------------------------------------------
` 
Run this function to compute the simulations of the model and to create the 
tables and figures
`
#-------------------------------------------------------------------------------


#Define path/file name to load Arg data
global Arg_Data_path        = "../../Empirics/00_data/02_output/";
global EMBI_data            =  "ARG_EMBI_quant.csv";
global Misreport_data       =  "InfMisreport_Series.txt";
global Output_data          =  "GDPcyc_ARG.csv";

#Load functions for simulations
include("01_Model_Structure.jl");
include("02_Functions.jl");
include("10_Simulations_Main_fx.jl");
include("11_Simulations_Moments_Unconditional.jl");
include("12_Simulations_Wrapper.jl");
using Interpolations, Optim, Roots, JLD, Plots
using Distributions, StatsBase

#-------------------------------------------------------------------------------
#Run Unconditional Simulation and Compute Moments
#-------------------------------------------------------------------------------
name_file = string("CE_model_structure_alpha_",0.028,".jld");
ce = load(string("model_data/",name_file), "ce");
target_mms, untgt_mms, piBE_mms, RP_mms, Moments_Ctype      = 
Simulations_Wrapper(ce; T=500_000, Initial_Type=1, do_plots=true, WINDOWS=true, Compute_η=true);
#------------------------------------------------------------------------------


#-------------------------------------------------------------------------------
# Import Argentine data; moments
#-------------------------------------------------------------------------------
data_mms    = readdlm(string(Arg_Data_path,"targets_out.txt"), ',', Float64);
data_piBE   = readdlm(string(Arg_Data_path,"piBE_out.txt"), ',', Float64);
data_elast  = readdlm(string(Arg_Data_path,"piBE_out.txt"), ',', Float64);
elast_data  = readdlm(string(Arg_Data_path,"Estimator_HAT_IV.txt"), ',', Float64)[1];
# Create vectors for data moments
target_dms  = [data_mms[9]*0.7; data_mms[8]*100; data_mms[5];     data_mms[6];      27;              elast_data];
untgt_dms   = [data_mms[1];     data_mms[3];     data_mms[2]*100; data_mms[4]*100;  data_mms[7]*100];
piBE_dms    = [data_piBE[1];    data_piBE[2];    0.290;           data_piBE[3]*100; data_piBE[4]*100; data_piBE[5]*100];
#-------------------------------------------------------------------------------


#-------------------------------------------------------------------------------
# Store results in .txt files to create .tex tables
#-------------------------------------------------------------------------------
     #Table 5
     open("simulated_moments/target_moments.txt", "w") do io
     writedlm(io, [target_dms target_mms], ',')
     end
     #Table 6
     open("simulated_moments/untgt_moments.txt", "w") do io
     writedlm(io, [untgt_dms untgt_mms], ',')
     end
     #Table 7
     open("simulated_moments/piBE_moments.txt", "w") do io
     writedlm(io, [piBE_dms piBE_mms], ',')
     end
     #Table 8
     open("simulated_moments/SPdecomp_moments.txt", "w") do io
     writedlm(io, RP_mms, ',')
     end
     #Table 9
     open("simulated_moments/spread_decomposition.txt", "w") do io
     writedlm(io, Moments_Ctype, ',')
     end
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
#Create Tables [also loads results from the perfect info case]
#------------------------------------------------------------------------------
include("31_Create_Tables.jl");
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
#Run Argentine Counterfactual - Compare with Data
#------------------------------------------------------------------------------
include("32_Arg_Counterfactual.jl");
#------------------------------------------------------------------------------
